/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import database.DbConnection;
import java.sql.ResultSet;

/**
 *
 * @author mrossetti
 */
public class Cliente {
    
    private int _IDCLIENTE;
    private String _RagSoc;
    private String _Indirizzo;
    private String _Telefoni;
    private String _Mail;
    private String _Note;
    private database.DbConnection Db;
    
    public Cliente(){
        _IDCLIENTE = -1;
        _RagSoc = "";
        _Indirizzo = "";
        _Telefoni = "";
        _Mail = "";
        _Note = "";
        Db = null;
    }
    
    public void setIDCLiente(int IDCLIENTE){
        _IDCLIENTE = IDCLIENTE;
    }
    
    public int getIDCliente(){
        return _IDCLIENTE;
    }
    
    public void setRagioneSociale(String RagioneSociale){
        _RagSoc = RagioneSociale;
    }
    
    public String getRagioneSociale(){
        return _RagSoc;
    }
    
    public void setIndirizzo(String Indirizzo){
        _Indirizzo = Indirizzo;
    }
    
    public String getIndirizzo(){
        return _Indirizzo;
    }
    
    public void setTelefoni(String Telefoni){
        _Telefoni = Telefoni;
    }
    
    public String getTelefoni(){
        return _Telefoni;
    }
    
    public void setMail(String Mail){
        _Mail = Mail;
    }
    
    public String getMail(){
        return _Mail;
    }
    
    public void setNote(String Note){
        _Note = Note;
    }
    
    public String getNote(){
        return _Note;
    }
    
    public void load(){
        Db = new DbConnection();
        if(getIDCliente() >= 0){
            ResultSet rs = Db.getResultSet("SELECT * FROM clienti WHERE IDCLIENTE = " + getIDCliente());
            try{
                if(rs.next()){
                    setRagioneSociale(rs.getString("ragsoc"));
                    setIndirizzo(rs.getString("indirizzo"));
                    setTelefoni(rs.getString("telefoni"));
                    setMail(rs.getString("mail"));
                    setNote(rs.getString("Note"));
                }
                rs.close();
                Db.CloseConnection();
            }catch(Exception e){
                e.printStackTrace();
                Db.CloseConnection();
            }
        }
    }
    
    public boolean save(){
        boolean res = false;
        Db = new DbConnection();
        String SQL = "";
        if(getIDCliente() < 0){
            SQL = "INSERT INTO clienti(ragsoc, indirizzo, telefoni, mail, Note)";
            SQL = SQL + "VALUES('" + getRagioneSociale().replaceAll("'", "''") + "', '" + getIndirizzo().replaceAll("'", "''") + "', '" + getTelefoni().replaceAll("'", "''") + "', '" + getMail().replaceAll("'", "''") + "', '" + getNote().replaceAll("'", "''") +"')";
        }else{
            SQL = SQL + "UPDATE clienti SET ";
            SQL = SQL + "ragsoc = '" + getRagioneSociale().replaceAll("'", "''") + "', ";
            SQL = SQL + "indirizzo = '" + getIndirizzo().replaceAll("'", "''") + "', ";
            SQL = SQL + "telefoni = '" + getTelefoni().replaceAll("'", "''") + "', ";
            SQL = SQL + "mail = '" + getMail().replaceAll("'", "''") + "', ";
            SQL = SQL + "Note = '" + getNote().replaceAll("'", "''") + "' ";
            SQL = SQL + "WHERE IDCLIENTE = " + getIDCliente();
        }
        if(Db.executeQuery(SQL))res = true;
        Db.CloseConnection();
        return res;
    }
    
    public boolean delete(){
        boolean res = false;
        Db = new DbConnection();
        String SQL = "";
        if(getIDCliente() >= 0){
            SQL = "DELETE FROM clienti WHERE IDCLIENTE = " + getIDCliente();
            if(Db.executeQuery(SQL))res = true;
        }
        Db.CloseConnection();
        return res;
    }
}
